package org.example.exchange.direct;

import com.rabbitmq.client.Channel;
import org.example.RabbitMQUtils;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * @description: C1 消费者：绑定 console 队列，routingKey 为 info、warning  C2 消费者：绑定 disk 队列，routingKey 为 error
 *               当生产者生产消息到 direct_logs 交换机里，该交换机会检测消息的 routingKey 条件，然后分配到满足条件的队列里，最后由消费者从队列消费消息。
 * @author: WangBlue
 * @create: 2023-11-29 20:23
 **/
public class ProducerDirectLogs {
    //交换机名称
    public static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMQUtils.getChannel();


        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String message = scanner.next();

            /**
             * 发送一个消息
             * 1.发送到哪个交换机
             * 2.路由的key值是哪个本次是队列的名称
             * 3.其他参数信息
             * 4.发送消息的消息体
             */
            switch (message) {
                case "info":
                    channel.basicPublish(EXCHANGE_NAME,"info",null,message.getBytes("UTF-8"));
                    break;
                case "warning":
                    channel.basicPublish(EXCHANGE_NAME,"info",null,message.getBytes("UTF-8"));
                    break;
                case "error":
                    channel.basicPublish(EXCHANGE_NAME,"error",null,message.getBytes("UTF-8"));
                    break;
                default:
                    System.out.println("只可以输入info、warning、error");

            }
//            channel.basicPublish(EXCHANGE_NAME,"error",null,message.getBytes("UTF-8"));
            System.out.println("生产者发出消息:"+message);
        }
    }
}
